package com.ChainBizPro.system.controller;

import com.ChainBizPro.system.common.BaseResponse;
import com.ChainBizPro.system.common.DeleteRequest;
import com.ChainBizPro.system.common.ErrorCode;
import com.ChainBizPro.system.common.ResultUtils;
import com.ChainBizPro.system.exception.BusinessException;
import com.ChainBizPro.system.exception.ThrowUtils;
import com.ChainBizPro.system.model.domain.Supplierratings;
import com.ChainBizPro.system.model.domain.Suppliers;
import com.ChainBizPro.system.model.domain.User;
import com.ChainBizPro.system.model.dto.supplierratings.SupplierratingsAddRequest;
import com.ChainBizPro.system.model.dto.supplierratings.SupplierratingsQueryRequest;
import com.ChainBizPro.system.model.dto.supplierratings.SupplierratingsUpdateMyRequest;
import com.ChainBizPro.system.model.vo.SupplierratingsVO;
import com.ChainBizPro.system.service.PurchaseOrdersService;
import com.ChainBizPro.system.service.SupplierratingsService;
import com.ChainBizPro.system.service.SuppliersService;
import com.ChainBizPro.system.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 供应商评分接口
 *
 * @author liusijia
 */
@RestController
@RequestMapping("/supplierrating")
@Api(tags = "供应商评分接口")
@Slf4j
public class SupplierratingsController {

    @Resource
    private PurchaseOrdersService purchaseOrdersService;

    @Resource
    private SuppliersService suppliersService;

    @Resource
    private UserService userService;

    @Resource
    private SupplierratingsService supplierratingsService;

    // region 增删改查

    /**
     * 创建供应商评分
     *
     * @param supplierratingsAddRequest
     * @param request
     * @return SupplierId
     */
    @PostMapping("/add")
    @ApiOperation(value = "创建供应商评分")
    public BaseResponse<Integer> addSupplierratings(@RequestBody SupplierratingsAddRequest supplierratingsAddRequest, HttpServletRequest request) {
        if (supplierratingsAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        QueryWrapper<Suppliers> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplierName", supplierratingsAddRequest.getSupplierName());
        Suppliers suppliers = suppliersService.getOne(queryWrapper);
        User loginUser = userService.getLoginUser(request);
        Supplierratings supplierratings = new Supplierratings();
        BeanUtils.copyProperties(supplierratingsAddRequest, supplierratings);
        supplierratings.setUserId(loginUser.getId());
        supplierratings.setSupplierId(suppliers.getSupplierId());
        boolean result = supplierratingsService.save(supplierratings);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "创建供应商评分失败");
        return ResultUtils.success(supplierratings.getRatingId());
    }

    /**
     * 删除供应商评分
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除供应商评分")
    public BaseResponse<Boolean> deleteSupplierratings(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        QueryWrapper<Supplierratings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ratingId", deleteRequest.getId());
        Supplierratings supplierratings = supplierratingsService.getOne(queryWrapper);
        User loginUser = userService.getLoginUser(request);
        boolean result = supplierratingsService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除失败");
        return ResultUtils.success(result);
    }

    /**
     * 更新供应商评分
     *
     * @param supplierratingsUpdateMyRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新供应商评分")
    public BaseResponse<Boolean> updateSupplierratings(@RequestBody SupplierratingsUpdateMyRequest supplierratingsUpdateMyRequest, HttpServletRequest request) {
        if (supplierratingsUpdateMyRequest == null || supplierratingsUpdateMyRequest.getRatingId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        QueryWrapper<Supplierratings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ratingId", supplierratingsUpdateMyRequest.getRatingId());
        Supplierratings supplierratings = supplierratingsService.getOne(queryWrapper);
        User loginUser = userService.getLoginUser(request);

        BeanUtils.copyProperties(supplierratingsUpdateMyRequest, supplierratings);
        boolean result = supplierratingsService.updateById(supplierratings);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "更新供应商评分失败");
        return ResultUtils.success(result);
    }

    /**
     * 分页获取全部用户评分(所有人)
     *
     * @param supplierratingsQueryRequest
     * @return
     */
    @PostMapping("/get/all")
    @ApiOperation(value = "分页获取全部用户评分(所有人)")
    public BaseResponse<Page<SupplierratingsVO>> getSupplierratingsByPage(@RequestBody SupplierratingsQueryRequest supplierratingsQueryRequest) {
        long current = supplierratingsQueryRequest.getCurrent() == 0 ? 1 : supplierratingsQueryRequest.getCurrent();
        long size = supplierratingsQueryRequest.getPageSize() == 0 ? 10 : supplierratingsQueryRequest.getPageSize();
        //分页信息封装Page对象
        Page<SupplierratingsVO> pageParam = new Page(current, size);
        Page<SupplierratingsVO> queryWrapper = supplierratingsService.getQueryWrapperByAll(pageParam, supplierratingsQueryRequest);
        return ResultUtils.success(queryWrapper);
    }
    // endregion

}
